package casbinService

import (
	"github.com/Sir-yuChen/funAnalyse/global"
	"github.com/Sir-yuChen/funAnalyse/model"
	"github.com/Sir-yuChen/funAnalyse/model/common/ApiReturn"
	"github.com/Sir-yuChen/funAnalyse/service/commonService"
	"github.com/Sir-yuChen/funAnalyse/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"gorm.io/gorm"
	"time"
)

type StockCasbinRoleService struct{}

func NewStockCasbinRoleService() *StockCasbinRoleService {
	return &StockCasbinRoleService{}
}

//GetStockCasbinRole 多条件查询
func (u *StockCasbinRoleService) GetStockCasbinRole(req *model.StockCasbinRoleModel) (a ApiReturn.ApiReturnCode) {
	var mo []model.StockCasbinRoleModel
	sql := ""
	obj := model.NewStockCasbinRoleModel()
	tableName := obj.TableName()
	sqlCondition := utils.ReflectHandleStuct(req)
	if sqlCondition != "" {
		sql = `select * from ` + tableName + ` where ` + sqlCondition
	} else {
		sql = `select * from ` + tableName + ` order by id desc  limit 100 `
	}
	err := global.GVA_DB.Raw(sql).Scan(&mo).Error
	if err != nil {
		global.GVA_LOG.Error("basis Get异常", zap.Any("req", req), zap.Any("sql", sql), zap.Error(err))
		return ApiReturn.ErrSystem
	}
	s := ApiReturn.OK
	if len(mo) == 0 {
		s = ApiReturn.NoData
	} else {
		s.Data = mo
	}
	return s
}

func (u *StockCasbinRoleService) InsertStockCasbinRole(req *model.StockCasbinRoleModel, c *gin.Context) (a ApiReturn.ApiReturnCode) {
	//角色名不能重复
	var t int64
	if err := global.GVA_DB.Model(model.StockCasbinRoleModel{}).Where("role_name = ?", req.RoleName).Count(&t).Error; err != nil {
		global.GVA_LOG.Error("InsertStockCasbinRole insert 校验角色名不能重复异常", zap.Any("request", req), zap.Error(err))
		return ApiReturn.ErrSystem
	}
	if t > 0 {
		global.GVA_LOG.Error("InsertStockCasbinRole insert 角色名不能重复", zap.Any("request", req))
		return ApiReturn.RoleNameRepeat
	}
	user, err := commonService.GetUserInfoByToken(c)
	if err != nil {
		global.GVA_LOG.Error("InsertStockCasbinRole 获取操作账户信息异常", zap.Any("request", req), zap.Error(err))
		return ApiReturn.ErrSystem
	}
	req.CreateBy = user.CustomerName
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		//Transaction 保证事务特性 使用tx对象
		err := tx.Model(req).Create(&req).Error
		if err != nil {
			global.GVA_LOG.Error("InsertStockCasbinRole insert异常", zap.Any("request", req), zap.Error(err))
			return err
		}
		//创建完角色，当前用户应该拥有自己创建的角色 同时为super配置角色关系
		//1. 当前用户的信息
		now := model.LocalTime(time.Now())
		reqCasbinRoleRelationUser := model.StockCasbinRoleRelationUserModel{
			RoleId:      req.Id,
			UserId:      user.CustomerId,
			Status:      "Y",
			CreateTime:  &now,
			LastUpdTime: &now,
		}
		if err = tx.Model(model.StockCasbinRoleRelationUserModel{}).Create(&reqCasbinRoleRelationUser).Error; err != nil {
			global.GVA_LOG.Error("InsertStockCasbinRole  操作账户绑定创建的新角色失败", zap.Any("request", req), zap.Error(err))
			return err
		}
		//返回nil提交事务
		return nil
	})
	if err != nil {
		return ApiReturn.ErrSystem
	}
	return ApiReturn.OK
}

//UpdateStockCasbinRole 更新
func (u *StockCasbinRoleService) UpdateStockCasbinRole(req *model.StockCasbinRoleModel) (a ApiReturn.ApiReturnCode) {
	if req.Id == 0 {
		return ApiReturn.ErrParam
	}
	err := global.GVA_DB.Model(req).Updates(req).Error
	if err != nil {
		global.GVA_LOG.Error("basis Update异常", zap.Any("req", req), zap.Error(err))
		return ApiReturn.ErrSystem
	}
	return ApiReturn.OK
}

//DelStockCasbinRole 根据ID删除
func (u *StockCasbinRoleService) DelStockCasbinRole(req *model.StockCasbinRoleModel) (a ApiReturn.ApiReturnCode) {
	if req.Id == 0 {
		return ApiReturn.ErrParam
	}
	err := global.GVA_DB.Model(req).Where("id = ?", req.Id).Delete(model.StockCasbinRoleModel{}).Error
	if err != nil {
		global.GVA_LOG.Error("basis Del异常", zap.Any("req", req), zap.Error(err))
		return ApiReturn.ErrSystem
	}
	return ApiReturn.OK
}
